﻿Function Assert-ComputerGroupFileVersionCoherency($referenceComputerName,$fileVersionInventoryPath)
{
    $fileVersionInventory=Import-csv $fileVersionInventoryPath

    $computersToValidate=$fileVersionInventory.ComputerName | select -Unique | Where-Object {$_ -ne $referenceComputerName} 
    $uniqueFileNames=$fileVersionInventory.Name | select -Unique
    $referenceComputerFilesVersion=$fileVersionInventory | Where-Object {$_.ComputerName -eq $referenceComputerName}


    foreach($file in $uniqueFileNames)
    {
        $referenceFile= $referenceComputerFilesVersion | Where-Object {$_.Name -eq $file}
        foreach($validatedComputer in $computersToValidate)
        {
               $validatedComputerFile=$fileVersionInventory | Where-Object {$_.ComputerName -eq $validatedComputer -and $_.Name -eq $file}

               if($validatedComputerFile.ProductVersion -ne $referenceFile.ProductVersion)
               {
                    New-Object -TypeName PSObject -Property @{ValidatedComputerName=$validatedComputer;
                                                            FileName=$file;
                                                            ValidatedComputerFileProductVersion=$validatedComputerFile.ProductVersion;
                                                            ReferenceComputerName=$referenceComputerName;
                                                            ReferenceComputerFileProductVersion=$referenceFile.ProductVersion;
                                                            Status="Differs from reference";}
               }
               else
               {
                    New-Object -TypeName PSObject -Property @{ValidatedComputerName=$validatedComputer;
                                                            FileName=$file;
                                                            ValidatedComputerFileProductVersion=$validatedComputerFile.ProductVersion;
                                                            ReferenceComputerName=$referenceComputerName;
                                                            ReferenceComputerFileProductVersion=$referenceFile.ProductVersion;
                                                            Status="Matches reference";}
               }
        }    
    }
}

Function Assert-ComputerGroupHotfixCoherency($referenceComputerName,$hotfixInventoryPath)
{
    $hotfixInventory=Import-csv $hotfixInventoryPath

    $computersToValidate=$hotfixInventory.PSComputerName | select -Unique | Where-Object {$_ -ne $referenceComputerName} 
    
    $referenceComputerHotfixes=($hotfixInventory | Where-Object {$_.PSComputerName -eq $referenceComputerName}).HotfixID

    
    foreach($validatedComputer in $computersToValidate)
    {
        $validatedComputerHotfixes=($hotfixInventory | Where-Object {$_.PSComputerName -eq $validatedComputer}).HotfixID

        $missingReferenceHotfix=$referenceComputerHotfixes | Where-Object {$_ -notin $validatedComputerHotfixes}
        $equalReferenceHotfix=$referenceComputerHotfixes | Where-Object {$_ -in $validatedComputerHotfixes}
        $addtionalValidatedComputerHotfix=$validatedComputerHotfixes| Where-Object {$_ -notin $referenceComputerHotfixes}

        $missingReferenceHotfix | foreach  -Process {New-Object -TypeName PSObject -Property @{ValidatedComputerName=$validatedComputer;
                                                                                                HotfixID=$_;
                                                                                                ReferenceComputerName=$referenceComputerName;
                                                                                                Status="Reference hotfix was not found on validated computer";} }

        
        $equalReferenceHotfix | foreach  -Process {New-Object -TypeName PSObject -Property @{ValidatedComputerName=$validatedComputer;
                                                                                                HotfixID=$_;
                                                                                                ReferenceComputerName=$referenceComputerName;
                                                                                                Status="Reference hotfix was found on validated computer";} }

        $addtionalValidatedComputerHotfix | foreach  -Process {New-Object -TypeName PSObject -Property @{ValidatedComputerName=$validatedComputer;
                                                                                                HotfixID=$_;
                                                                                                ReferenceComputerName=$referenceComputerName;
                                                                                                Status="Hotfix was not found on reference computer";} }
            
    }
    
}